package cn.yitun.chat.yitunserverimapicore.business.notice.service.impl;

import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.notice.TextNoticeDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.notice.UserTextNoticeDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.notice.TextNoticeInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.notice.TextNoticeVo;
import cn.yitun.chat.yitunservercommonlib.enums.system.TextNoticeTypeEnums;
import cn.yitun.chat.yitunserverimapicore.business.notice.service.TextNoticeService;
import cn.yitun.chat.yitunserverimbase.dao.notice.TextNoticeDao;
import cn.yitun.chat.yitunserverimbase.dao.notice.UserTextNoticeDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yitun<br>
 * @version 1.1
 */
@Service
@Transactional
public class TextNoticeServiceImpl
		extends CrudServiceImpl<TextNoticeDao, TextNoticeDto, TextNoticeInfo, TextNoticeVo>
		implements TextNoticeService {

	@Resource
	UserTextNoticeDao userTextNoticeDao;

	public void save(TextNoticeDto textNotice, List<Long> userIds, String pushType) {
		if (null != textNotice) {
			if (YitunCollectionUtil.isEmpty(userIds)) {
				textNotice.setPushType(TextNoticeTypeEnums.PUSH_TYPE_ALL.getType());
				dao.save(textNotice);
				Long textNoticeId = textNotice.getId();
				userTextNoticeDao.insertPush(textNoticeId, userIds);
			} else {
				textNotice.setPushType(TextNoticeTypeEnums.PUSH_TYPE_PART.getType());
				dao.save(textNotice);

				Long textNoticeId = textNotice.getId();
				if (TextNoticeTypeEnums.PUSH_TYPE_ALL.getType().equals(pushType)) {
					for (Long userId : userIds) {
						UserTextNoticeDto utn = new UserTextNoticeDto();
						utn.setUserId(userId);
						utn.setTextNoticeId(textNoticeId);
						utn.setIsRead(false);
						userTextNoticeDao.save(utn);
					}
				} else {
					userTextNoticeDao.insertPush(textNoticeId, userIds);
				}
			}
		}
	}

	/**
	 * 获取列表
	 */
	public List<TextNoticeVo> getTextNoticeList(Long userId) {
		return dao.getTextNoticeList(userId);
	}
}
